package com.asion.vmovie.ui.MVPModel;

import com.asion.vmovie.api.ApiService;
import com.asion.vmovie.beans.DetialsBean;
import com.asion.vmovie.beans.MuHouTabBean;
import com.asion.vmovie.ui.MVPContract.MuHouContract;
import com.asion.vmovie.utils.retrofitCache.SetCacheUtils;

import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by asion on 16-10-18.
 */

public class MuHouModel implements MuHouContract.Model {
    @Override
    public void getTabData(MuHouContract.OnTabDataPost onTabDataPost) {
        Retrofit retrofit = new Retrofit.Builder().addCallAdapterFactory(RxJavaCallAdapterFactory.create()).addConverterFactory(GsonConverterFactory.create()).client(SetCacheUtils.setCache()).baseUrl(ApiService.BASE_URL).build();
        ApiService apiService = retrofit.create(ApiService.class);
        Observable<MuHouTabBean> backStageTab = apiService.getBackStageTab();
        backStageTab.subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(muHouTabBean -> onTabDataPost.onDataPostSuccess(muHouTabBean)
                        ,throwable -> onTabDataPost.onDataPostError(throwable));
    }

    @Override
    public void getDetialData(int p,int size,int cateid, MuHouContract.OnDetialDataPost onDetialDataPost) {
        Retrofit retrofit = new Retrofit.Builder().addCallAdapterFactory(RxJavaCallAdapterFactory.create()).addConverterFactory(GsonConverterFactory.create()).client(SetCacheUtils.setCache()).baseUrl(ApiService.BASE_URL).build();
        ApiService apiService = retrofit.create(ApiService.class);
        Observable<DetialsBean> backStageDetial = apiService.getBackStageDetial(p, size, cateid);
        backStageDetial.subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(detialsBean -> onDetialDataPost.onDataPostSuccess(detialsBean)
                ,throwable -> onDetialDataPost.onDataPostError(throwable));
    }
}
